//
//  MineAttentionView.swift
//  maosheng
//
//  Created by lxl on 2019/8/12.
//

import UIKit
import Alamofire
class MineAttentionView: PublicSwiftTableView {
    let cellIdentifer = "AttentionCell"
    var isFans : Bool = false
    var str_url = "api/services/app/Follower/GetMyFollowedPagingListAsync"
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
      
        tableView.register(AttentionCell.self, forCellReuseIdentifier: cellIdentifer)
        getTheAttentionList(isHeader: true)
        refreshList = {[weak self] (isHeader) in
            self?.getTheAttentionList(isHeader: isHeader)

        }

        
    }
    func getTheAttentionList(isHeader : Bool) -> Void {
        let url =  str_url + "?PageIndex=\(pageIndex)&PageSize=10"

        SDataCenter.shared.SGetJsonDataWithUrl(str_url: url, params: nil) { [weak self] (responseData, isSuccess) in
            if self?.tableView.mj_header != nil{
                self?.tableView.mj_header.endRefreshing()
                self?.tableView.mj_footer.endRefreshing()
            }
            
            if isSuccess && responseData is [String : Any]{
                if isHeader{
                    self?.dataSource.removeAllObjects()
                }
                if let items = (responseData as! [String : Any])["items"]{
                    if let arr = items as? Array<Any>{
                       if let attentionArr = [AttentinModel].deserialize(from: arr){
                        self?.dataSource.addObjects(from: attentionArr as [Any])
                        }
                    }
                    
                }
                self?.checkNeedNoDataView()
                self?.setRefreshFooter()
            }else{
                if !isHeader{
                    self?.pageIndex = self!.pageIndex - 1
                }
                
            }
        }
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifer) as! AttentionCell
//        cell
        let model = self.dataSource[indexPath.row]
        cell.setTheAttentionInfo(model: model as! AttentinModel)
        cell.attentionBtn.addTarget(self, action: #selector(unfollowOrFollow(btn:)), for: .touchUpInside)
        cell.attentionBtn.tag = 101 + indexPath.row
        return cell
    }
    
    @objc func unfollowOrFollow(btn:UIButton)  {
        PublicTools.showAlert(currentVC: nil, meg: "取消关注？", cancelBtn: "取消", otherBtn: "确定") {[weak self] (action) in
            self?.cancleAttention(btn: btn)
        }
        
    }
    func cancleAttention(btn: UIButton){
        let btnTag = btn.tag - 101
        let delUrl = "api/services/app/Follower/CancelFollowAsync"
        let model = self.dataSource[btnTag] as? AttentinModel
        var params = [String : Any]()
        params["moduleTypeId"] = 100101
        params["followedObjectId"] = model?.followedObjectId
        
        SDataCenter.shared.SPostJsonDataWithUrl(str_url: delUrl, params: params) { [weak self] (responseData, isSuccess) in
            if isSuccess{
                self?.dataSource.removeObject(at: btnTag)
                self?.tableView.reloadData()
                self?.checkNeedNoDataView()
            }
        }
    }
    
    func checkNeedNoDataView(){
        if self.dataSource.count == 0 {
            let str_msg : String = self.isFans == false ? "还没有任何关注哦" : "还没有任何粉丝哦"
            self.showTheNoDataView(title: str_msg)
        }else{
            if self.noDataView != nil{
                self.noDataView?.isHidden = true
            }
        }
    }
}
